<?php

class youtube {
	
	/**
	 * 
	 * @param string $q
	 * @param integer $startindex
	 * @param integer $maxresults
	 * @return video_collection
	 */
	static function getVideos($q, $startindex, $maxresults) {	
		$result = array();
		$videos = new video_collection();
				
		// get feed
		$feed = new youtube_list($q, $startindex, $maxresults);
		
		// loop entries
		foreach ($feed->entries as $entry) {
			/* @var $video video */
			$video = new video($entry["id"]);
			// update from feed
			if (strtotime($video->updatedon) < strtotime("-" . config::getInstance()->video_ttl . " days") || !$video->hasUpdatedon()) {
				self::updateVideo($video, $entry);
				$video->addToMusicBrainzCue();
			}
			$videos->addItem($video);
		}
		if (musicbrainz::hasCue()) {
			musicbrainz::runCueAsync();
		}
				
		return $videos;
	}
	
	static function updateVideo(video $video, $entry) {
		$video->setAll($entry);						
		$video->parse(false);		
		$video->setUpdated(false);		
		$video->getMetaDataObject()->save();
		$video->save();
	}

}

?>